package com.client.ryt.activity;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.client.ryt.R;
import com.client.ryt.basic.BaseActivity;
import com.client.ryt.basic.BaseException;
import com.client.ryt.basic.BaseLiteners;
import com.client.ryt.basic.BaseResponse;
import com.client.ryt.basic.BaseTask;
import com.client.ryt.config.LoginConfig;
import com.client.ryt.entity.parameter.CheckCardParameters;
import com.client.ryt.task.http.request.CheckCardRequest;
import com.client.ryt.task.http.response.CheckCardResponse;
import com.client.ryt.utils.SysUtil;
import com.client.ryt.widget.scaner.MipcaActivityCapture;

/****
 * 直接输入交易码
 */
public class CashCodeActivity extends BaseActivity {
    private final static int SCANNIN_GREQUEST_CODE = 1;
    EditText etCashCode = null;
    Button btnCheckcode;
    Button btnDocash= null;
    TextView tvErrorMsg = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_cash_code);
        etCashCode = (EditText)findViewById(R.id.et_inputcashcode);
        btnCheckcode = (Button)findViewById(R.id.btn_checkcode);
        btnDocash = (Button)findViewById(R.id.btn_docash);
        tvErrorMsg = (TextView)findViewById(R.id.tv_error_msg);
        btnCheckcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkCard(etCashCode.getText().toString());
            }
        });

        btnDocash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CashCodeActivity.this,CashDoneActivity.class);
                startActivity(intent);
                finish();
            }
        });

        etCashCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {}

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { }

            @Override
            public void afterTextChanged(Editable editable) {
                if(editable.length() > 0){
                    btnCheckcode.setVisibility(View.VISIBLE);
                    btnDocash.setVisibility(View.GONE);
                }else{
                    btnCheckcode.setVisibility(View.GONE);
                    btnDocash.setVisibility(View.VISIBLE);
                }
            }
        });
       // startCash();

        Intent it = getIntent();
        if(it.hasExtra("error_msg")){
            tvErrorMsg.setText(it.getStringExtra("error_msg"));
        }

    }


    private void checkCard(String card){
        CheckCardParameters checkCardParameters= new CheckCardParameters();
        checkCardParameters.setToken(LoginConfig.getLoginConfig().getToken());
        checkCardParameters.setUsername(LoginConfig.getLoginConfig().getShopMember().getUsername());
        checkCardParameters.setShopid(LoginConfig.getLoginConfig().getShopMember().getShop_id());
        checkCardParameters.setCardsn(card);
        new BaseTask<CheckCardRequest,CheckCardResponse>(
                CashCodeActivity.this,CheckCardRequest.class,CheckCardResponse.class,checkCardParameters,new BaseLiteners.BaseResponseListener() {
            @Override
            public void OnFail(BaseException e) {
                showToast(e.getErrmsg());
            }

            @Override
            public void OnSuccess(BaseResponse o) {
                if(o.getIsSuccess()){
                    //todo
                    Intent intent = new Intent(CashCodeActivity.this,CashDoneActivity.class);
                    intent.putExtra("card_sn",etCashCode.getText().toString());
                    startActivity(intent);
                    showToast("卡密验证成功");
                    finish();
                }
            }
        },true).execute();

    }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case SCANNIN_GREQUEST_CODE:
                if(resultCode == RESULT_OK){
                    Bundle bundle = data.getExtras();
                    //显示扫描到的内容
                    checkCard(bundle.getString("result"));
                }
                break;
        }
    }


    private void startCash(){
        Intent intent = new Intent();
        intent.setClass(CashCodeActivity.this, MipcaActivityCapture.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivityForResult(intent, SCANNIN_GREQUEST_CODE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.cash_code, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            startCash();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
